//--------------------------------------------------------------------------------
// This file is a portion of the D2D1Renderer.  It is distributed
// under the MIT License, available in the root of this distribution and 
// at the following URL:
//
// http://www.opensource.org/licenses/mit-license.php
//
// Copyright (c) Mirco Timmermann
//
//--------------------------------------------------------------------------------
#include "ProgressBar.h"


ProgressBar::ProgressBar(int x, int y, int width, int height) {
//data
	_minValue = 0;
	_maxValue = 100;

	_value_norm = 0; //[0..1]

//visualisation
	this->_x = x;
	this->_y = y;
	this->_width = width;
	this->_height = height;

	_color = Color(0, 1, 0);
}

ProgressBar::~ProgressBar(void) {
	
}

/////////////////////////////DATA////////////////////////////

void ProgressBar::SetValue_norm(float value) {
	//clamp value.
    if(value < 0) value = 0;
	if(value > 1) value = 1;

	_value_norm = value;
}

float ProgressBar::GetValue_norm() {
	return _value_norm;
}

void ProgressBar::SetValue(int value) {
	//clamp value.
    if(value < _minValue) value = _minValue;
    if(value > _maxValue) value = _maxValue;
        
    //calculate number to the normed value.
    int length = _maxValue - _minValue; //length = distance.
    _value_norm = 1/(float)length*(value-_minValue); //normalize
}

int ProgressBar::GetValue() {
	return interpolate(_value_norm);
}

int ProgressBar::interpolate(float value_norm) {
	return (value_norm*(_maxValue)) + ((1 - value_norm)*_minValue);
}

/////////////////////////////////////////////////////////////

void ProgressBar::SetColor(float r, float g, float b) {
	_color.r = r;
	_color.g = g;
	_color.b = b;
}

bool ProgressBar::Draw(Direct2DContext* ctx, D2D1::Matrix3x2F* mtx) {
	int step = _width*_value_norm;

	ctx->lineWidth = 1;
	ctx->SetStrokeStyle(_color.r, _color.g, _color.b);
	ctx->fillRect(0, 0, (float)step, (float)this->_height);

	return true;
}